#!/usr/bin/perl -w

use strict;
use warnings;
use Data::Dumper;

our $path = 'Style';

open (FB , "css2_properties_index.html");
      my @d = <FB>;
close FB;

my $t = join "" , @d;
my @res = ();

while ($t =~ /<a href="([^"]+)" class="noxref"><span class="([^"]+)">'([^']+)'/g) {
      push @res , $3;
}


my %seen = ();
my @list = grep { ! $seen{$_} ++ } @res;

@list = sort { $a cmp $b } @list;
push @list , "";

#print Dumper \@list;
#die;

&process_same_styles(\@list);

sub process_same_styles { 
      my ($names) = @_;
      
      for (my $i = 0; $i <= $#{$names}; $i++) {
            my @same = ();
            my $name = $names->[$i];
            if ($name =~ /-/) {
                  $name = (split /-/ , $name)[0];
            }
            for (my $j = $i; $j <= $#{$names}; $j++) {
                  my $against = $names->[$j];
                  if ($name eq $against) {
                        push @same , $name;
                  } else {
                        if ($against =~ /^$name/) {
                              push @same , $against;
                        } else {
                              &generate(\@same) if @same;
                              $i = $j - 1;
                              last;
                        }
                  }
            }
      }
}

sub generate {
      my ($list) = @_;
      
      #print Dumper $list;
      #return;
      
      my $module_name = "";
      if ($list->[0] =~ /-/) {
            $module_name = (split /-/ , $list->[0])[0]; 
      } else {
            $module_name = $list->[0];
      }
      
      print "Generating module: " , ucfirst($module_name) , ".pm\n";
      if (-e $path . "/" . $module_name . ".pm") {
            print "\t\tAlready exists\n";
      } else {
            
            open (FH , ">>$path/". ucfirst($module_name) . ".pm");
                  print FH "package CSS::Builder::Style::". ucfirst($module_name) . ";\n";
                  print FH "use Moose;\n\n";
                  
                  print FH "extends 'CSS::Builder::Style';\n\n";
                  
                  print FH "has 'name'        => ( isa => 'Str' , is => 'ro' , default => \'$module_name\' ); # we don't need to change that, does we?\n";
                  print FH "has \'$module_name\'  => ( isa => 'Any' , is => 'rw' ); #default object\n";
                  
                  if ($list->[0] =~ /-/) {
                              $list->[0] =~ /(.+)-(.+)/;
                              my $prop = $2;
                              if ($prop) {
                                    print FH "has \'$prop\'        => ( isa => 'Any' , is => 'rw' );\n";
                              }
                  }
                  
                  if ($#{$list}) {
                        
                        foreach my $i (1 .. $#{$list}) {
                              
                              my $p = $list->[$i];
                              if ($p) {
                                    
                                    $p =~ /$module_name-(.+)/;
                                    my $prop = $1;
                                    
                                    $prop =~ s/-/_/g;
                                    
                                    if ($prop) {
                                          
                                          print FH "has \'$prop\'        => ( isa => 'Any' , is => 'rw' );\n";
                                          
                                    }
                                    
                              }
                              
                        }
                        
                  } 
                  
                  #foreach my $i (0 .. $#{$list}) {
                  #      if ($list->[$i])
                  #}
                  
                  print FH "\n\n1;";
                  
                  
            close FH;
      }
      
      
      #print Dumper $list;
}




